package com.jia.leetCode;

public class Pro0679 {
    public boolean judgePoint24(int[] nums) {
        double a = nums[0];
        double b = nums[1];
        double c = nums[2];
        double d = nums[3];
        return judge(a, b, c, d);
    }
    public boolean judge(double a, double b, double c, double d){
        return
                // a b
                judge(a + b, c, d) ||
                        judge(a * b, c, d) ||
                        judge(a - b, c, d) ||
                        judge(b - a, c, d) ||
                        judge(a / b, c, d) ||
                        judge(b / a, c, d) ||
                        // b c
                        judge(c + b, a, d) ||
                        judge(c * b, a, d) ||
                        judge(c - b, a, d) ||
                        judge(b - c, a, d) ||
                        judge(c / b, a, d) ||
                        judge(b / c, a, d) ||
                        // c d
                        judge(c + d, a, b) ||
                        judge(c * d, a, b) ||
                        judge(c - d, a, b) ||
                        judge(d - c, a, b) ||
                        judge(c / d, a, b) ||
                        judge(d / c, a, b) ||
                        // b d
                        judge(b + d, a, c) ||
                        judge(b * d, a, c) ||
                        judge(b - d, a, c) ||
                        judge(d - b, a, c) ||
                        judge(b / d, a, c) ||
                        judge(d / b, a, c) ||
                        // a c
                        judge(a + c, b, d) ||
                        judge(a * c, b, d) ||
                        judge(a - c, b, d) ||
                        judge(c - a, b, d) ||
                        judge(a / c, b, d) ||
                        judge(c / a, b, d) ||
                        // a d
                        judge(a + d, b, c) ||
                        judge(a * d, b, c) ||
                        judge(a - d, b, c) ||
                        judge(d - a, b, c) ||
                        judge(a / d, b, c) ||
                        judge(d / a, b, c) ;
        //
    }
    public boolean judge(double a, double b, double c){// 24 , 3 , 8
        return
                judge(a + b, c) ||
                        judge(a * b, c) ||
                        judge(a - b, c) ||
                        judge(b - a, c) ||
                        judge(a / b, c) ||
                        judge(b / a, c) ||
                        //
                        judge(c + b, a) ||
                        judge(c * b, a) ||
                        judge(c - b, a) ||
                        judge(b - c, a) ||
                        judge(c / b, a) ||
                        judge(b / c, a) ||
                        //
                        judge(c + a, b) ||
                        judge(c * a, b) ||
                        judge(c - a, b) ||
                        judge(a - c, b) ||
                        judge(c / a, b) ||
                        judge(a / c, b);
    }

    public boolean judge(double a, double b){
        return
                Math.abs(a + b - 24) < 0.001 ||
                        Math.abs(a - b - 24) < 0.001 ||
                        Math.abs(b - a - 24) < 0.001 ||
                        Math.abs(a * b - 24) < 0.001 ||
                        Math.abs(a / b - 24) < 0.001 ||
                        Math.abs(b / a - 24) < 0.001;
    }

}
